#include <ros/ros.h>
#include <gtest/gtest.h>
#include <string>

#include "canbus/handler.h"
#include <iostream>

using namespace canbus;

TEST(CanbusHandleTest, HandleConstruction)
{
  {
    std::string id = "test handler";
    int channel = 0;
    long can_rate = canBITRATE_250K;
    int can_msg_type = canMSG_STD;
    CanbusHandler hdl(id, channel, can_rate, can_msg_type);
    EXPECT_EQ("test handler", hdl.getID());
  }
}


TEST(CanbusMsgTest, CanMsgConstruction)
{
  uint8_t val[8] = {11,22,33,44,55,66,77,88};
  CanMsg20 msg20(val);
  uint8_t* val_out;
  val_out = msg20.getBytes();
  for(int i=0;i<8;i++) {
    std::cout << (int)*(val_out+i) << std::endl; 
  }
  EXPECT_EQ(val[8], *val_out);
}


/*
class ImuSensorInterfaceTest : public ::testing::Test
{
public:
  ImuSensorInterfaceTest()
    : name1("name_1"), name2("name_2"),
      frame_id1("frame_1"), frame_id2("frame_2")
  {
    srand(time(NULL)); // Seed random number generator

    // Populate raw data
    for (unsigned int i = 0; i < 4; ++i)
    {
      orientation1[i] = randomDouble();
      orientation2[i] = randomDouble();
    }
    for (unsigned int i = 0; i < 3; ++i)
    {
      angular_velocity1[i]    = randomDouble();
      linear_acceleration1[i] = randomDouble();
    }
    for (unsigned int i = 0; i < 9; ++i)
    {
      orientation_covariance1[i]         = randomDouble();
      angular_velocity_covariance1[i]    = randomDouble();
      linear_acceleration_covariance1[i] = randomDouble();
    }

    // First handle exposes all fields
    ImuSensorHandle::Data data1;
    data1.name                           = name1;
    data1.frame_id                       = frame_id1;
    data1.orientation                    = orientation1;
    data1.orientation_covariance         = orientation_covariance1;
    data1.angular_velocity               = angular_velocity1;
    data1.angular_velocity_covariance    = angular_velocity_covariance1;
    data1.linear_acceleration            = linear_acceleration1;
    data1.linear_acceleration_covariance = linear_acceleration_covariance1;
    h1 = ImuSensorHandle(data1);

    // Second handle only exposes orientation data
    ImuSensorHandle::Data data2;
    data2.name        = name2;
    data2.frame_id    = frame_id2;
    data2.orientation = orientation2;
    h2 = ImuSensorHandle(data2);
  }

protected:
  double orientation1[4], orientation2[4];
  double orientation_covariance1[9];
  double angular_velocity1[3];
  double angular_velocity_covariance1[9];
  double linear_acceleration1[3];
  double linear_acceleration_covariance1[9];
  string name1, name2;
  string frame_id1, frame_id2;
  ImuSensorHandle h1;
  ImuSensorHandle h2;

  double randomDouble(double min_val = 0.0, double max_val = 1.0)
  {
    const double range = max_val - min_val;
    return rand() / static_cast<double>(RAND_MAX) * range + min_val;
  }
};

TEST_F(ImuSensorInterfaceTest, ExcerciseApi)
{
  ImuSensorInterface iface;
  iface.registerHandle(h1);
  iface.registerHandle(h2);

  // Get handles
  EXPECT_NO_THROW(iface.getHandle(name1));
  EXPECT_NO_THROW(iface.getHandle(name2));

  ImuSensorHandle h1_tmp = iface.getHandle(name1);
  EXPECT_EQ(name1, h1_tmp.getName());
  EXPECT_EQ(frame_id1, h1_tmp.getFrameId());

  EXPECT_TRUE(0 != h1_tmp.getOrientation());
  EXPECT_TRUE(0 != h1_tmp.getOrientationCovariance());
  EXPECT_TRUE(0 != h1_tmp.getAngularVelocity());
  EXPECT_TRUE(0 != h1_tmp.getAngularVelocityCovariance());
  EXPECT_TRUE(0 != h1_tmp.getLinearAcceleration());
  EXPECT_TRUE(0 != h1_tmp.getLinearAccelerationCovariance());

  for (unsigned int i = 0; i < 4; ++i)
  {
    EXPECT_DOUBLE_EQ(orientation1[i], h1_tmp.getOrientation()[i]);
  }

  for (unsigned int i = 0; i < 3; ++i)
  {
    EXPECT_DOUBLE_EQ(angular_velocity1[i],    h1_tmp.getAngularVelocity()[i]);
    EXPECT_DOUBLE_EQ(linear_acceleration1[i], h1_tmp.getLinearAcceleration()[i]);
  }

  for (unsigned int i = 0; i < 9; ++i)
  {
    EXPECT_DOUBLE_EQ(orientation_covariance1[i],         h1_tmp.getOrientationCovariance()[i]);
    EXPECT_DOUBLE_EQ(angular_velocity_covariance1[i],    h1_tmp.getAngularVelocityCovariance()[i]);
    EXPECT_DOUBLE_EQ(linear_acceleration_covariance1[i], h1_tmp.getLinearAccelerationCovariance()[i]);
  }

  ImuSensorHandle h2_tmp = iface.getHandle(name2);
  EXPECT_EQ(name2, h2_tmp.getName());
  EXPECT_EQ(frame_id2, h2_tmp.getFrameId());

  EXPECT_TRUE(0 != h2_tmp.getOrientation());
  EXPECT_TRUE(0 == h2_tmp.getOrientationCovariance());
  EXPECT_TRUE(0 == h2_tmp.getAngularVelocity());
  EXPECT_TRUE(0 == h2_tmp.getAngularVelocityCovariance());
  EXPECT_TRUE(0 == h2_tmp.getLinearAcceleration());
  EXPECT_TRUE(0 == h2_tmp.getLinearAccelerationCovariance());

  for (unsigned int i = 0; i < 4; ++i)
  {
    EXPECT_DOUBLE_EQ(orientation2[i], h2_tmp.getOrientation()[i]);
  }

  // This interface does not claim resources
  EXPECT_TRUE(iface.getClaims().empty());

  // Print error message
  // Requires manual output inspection, but exception message should contain the interface name (not its base class)
  try {iface.getHandle("unknown_name");}
  catch(const HardwareInterfaceException& e) {ROS_ERROR_STREAM(e.what());}
}
*/

int main(int argc, char** argv)
{
  testing::InitGoogleTest(&argc, argv);
  return RUN_ALL_TESTS();
}
